<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */
if (!defined('IN_DOUCO_API')) {
    die('Access Denied');
}

/**
 * 生成订单
 */
function doPageCreate()
{

    global $_GPC, $_W, $check, $_LANG;

    $user_id = $_GPC['user_id'] ? $_GPC['user_id'] : get_user_by_openid(wxapp_getopenid());
    $product_id = $_GPC['product_id'];
    $product_number = max(1, $_GPC['product_number']);

    $contact = $_GPC['contact'];
    $telphone = $_GPC['telphone'];
    $address = $_GPC['address'];
    $remark = $_GPC['remark'];
    $update_user_information = $_GPC['update_user_information'];

    if (!$user_id) {
        return wxapp_result(41009, '请登录');
    }
    if (!$product_id) {
        return wxapp_result(404, '产品不存在');
    }

    //验证联系人信息
    if (!$check->is_telphone($telphone)) {
        return wxapp_result(404, $_LANG['user_telphone_cue']);
    }
    if (!$contact) {
        return wxapp_result(404, $_LANG['user_contact_empty']);
    } elseif ($check->is_illegal_char($contact)) {
        return wxapp_result(404, $_LANG['user_contact'] . $_LANG['illegal_char']);
    }
    // 验证地址
    if (!$address) {
        return wxapp_result(404, $_LANG['user_address_empty']);
    } elseif ($check->is_illegal_char($_POST['address'])) {
        return wxapp_result(404, $_LANG['user_address'] . $_LANG['illegal_char']);
    }

    if ($update_user_information == 'yes') {
        pdo_update('user', array('contact' => $contact, 'telphone' => $telphone, 'address' => $address), array('user_id' => $user_id));
    }
    //开始生成订单

    $product = get_item('product', $product_id);

    $order['shipping_id'] = 'wxapp';
    $order['shipping_fee'] = 0;
    $order['order_sn'] = create_order_sn();
    $order['user_id'] = $user_id;
    $order['telphone'] = $telphone;
    $order['contact'] = $contact;
    $order['address'] = $address;
    $order['remark'] = $remark;
    $order['pay_id'] = 'wxapp';
    $order['product_amount'] = $product['price'] * $product_number;
    $order['order_amount'] = $order['product_amount'];
    $order['add_time'] = time();
    pdo_insert('order', $order);
    $order_id = pdo_insertid();
    //插入商品
    $order_product['order_id'] = $order_id;
    $order_product['product_id'] = $product_id;
    $order_product['name'] = $product['name'];
    $order_product['price'] = $product['price'];
    $order_product['product_number'] = $product_number;
    $order_product['defined'] = $product['defined'];
    pdo_insert('order_product', $order_product);
    //返回订单
    return wxapp_result(0, '订单生成成功', $order);

}

/**
 * 订单列表
 */
function doPageList(){

    global $_GPC,$_W;
    $status = $_GPC['status'];
    $user_id = $_GPC['user_id'] ? $_GPC['user_id'] : get_user_by_openid(wxapp_getopenid());
    $pindex = max(1, intval($_GPC["page"]));
    $cat_id =  intval($_GPC["cid"]);
    $psize = 10


    ;

    $where = " WHERE user_id = :user_id";
    $params['user_id'] = $user_id;
    if($status != 9){
        $where .= " AND status = :status";
        $params[':status'] = $status;
    }

    $data['num'] = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename("order") . $where,$params);
    $data['page_size'] = $psize;
    $data["list"] = pdo_fetchall("SELECT * FROM " . tablename("order") .  $where . " ORDER BY order_id DESC LIMIT " . ($pindex - 1) * $psize . "," . $psize,$params);


    foreach ($data['list'] as $key=>$value){

        $order_product = pdo_get('order_product', array('order_id' => $value['order_id']));
        $product = get_item('product',$order_product['product_id']);
        $product['num'] = $order_product['product_number'];
        $product['image'] = ROOT_URL.$product['image'];

        $data['list'][$key]['product'] = $product;
    }

    return wxapp_result(0,'success',$data);


}
/**
 * 获取订单
 */
function doPageGet()
{
    global $_GPC, $_W, $_LANG;
    $order_sn = $_GPC['order_sn'];
    $user_id = $_GPC['user_id'] ? $_GPC['user_id'] : get_user_by_openid(wxapp_getopenid());
    $order = pdo_get('order', array('order_sn' => $order_sn, 'user_id' => $user_id));
    if (!$order) {
        return wxapp_result(404, '订单不存在');
    }
    $order['add_time'] = date("Y-m-d h:i:s", $order['add_time']);
    $order['status_format'] = $_LANG['order_status_' . $order['status']];
    $order_product = pdo_get('order_product', array('order_id' => $order['order_id']));
    $product = get_item('product',$order_product['product_id']);
    $order_product['image'] = ROOT_URL.$product['image'];
    $order['product'] = $order_product;
    return wxapp_result(0, 'success', $order);
}

/**
 * 取消订单
 */
function doPageCancel(){
    global $_GPC, $_W, $_LANG;
    $order_sn = $_GPC['order_sn'];
    $user_id = $_GPC['user_id'] ? $_GPC['user_id'] : get_user_by_openid(wxapp_getopenid());
    $order = pdo_get('order', array('order_sn' => $order_sn, 'user_id' => $user_id));
    if (!$order) {
        return wxapp_result(404, '订单不存在');
    }
    pdo_update('order',array('status'=>-1),array('order_sn' => $order_sn, 'user_id' => $user_id));
    return wxapp_result(0, 'success');
}
/**
 * 支付订单
 */
function doPagePay()
{
    global $_GPC, $_W, $_LANG;
    $order = pdo_get('order', array('order_id' => $_GPC['order_id']));
    $openid = $_GPC['openid'] ? $_GPC['openid'] : wxapp_getopenid();
    if (!$order) {
        return wxapp_result(404, '订单不存在');
    }
    load()->classs('wxpay');

    //支付参数
    $appid = $_W['setting']['wxapp_key'];
    $mch_id = $_W['setting']['wxapp_mchid'];      //商户id
    $key = $_W['setting']["wxapp_signkey"];
    $out_trade_no =$order['order_sn'];
    $body = "商品支付，订单编号：".$out_trade_no;
    $total_fee = $order["order_amount"] * 100;
    $notify_url = ROOT_URL.API_PATH.'/callback.php';
    $weixinpay = new WeixinPay($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee,$notify_url);
    $return = $weixinpay->pay();
    return wxapp_result(0,'success',$return);
}

